﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Z1029.NoteEditor
{
    public class EventLine : MonoBehaviour
    {
        public GameObject beatline;
        public GameObject litBeatline;
        public Beatline[] beatlines;
        public Beatline[] litBeatlines;

        public JudgementLine judgeline;

        public EventBase eventPrefab;
        public Transform speedsParent;
        public Transform movesParent;
        public Transform rotatesParent;
        public Transform alphasParent;

        public EventBase[] speeds;
        public EventBase[] moves;
        public EventBase[] rotates;
        public EventBase[] alphas;

        private void Start()
        {
            judgeline = LevelController.instance.judgelines[0];
            InitBeatlines();
            InitEvents();
        }

        public void InitEvents()
        {
            List<Z1029.NoteGlobalView.ValueSet> vs;
            int i;
            vs = judgeline.alphas;
            for (i=0;i<vs.Count;i++)
            {
                EventBase eb = Instantiate(eventPrefab.gameObject, alphasParent).GetComponent<EventBase>();
                EventBase.EventData d = new EventBase.EventData();
                d.time = vs[i].startTime;
                d.duration = vs[i].endTime - vs[i].startTime;
                d.value = vs[i];
                eb.eventData = d;
                eb.type = EventType.Alpha;
            }
            vs = judgeline.moves;
            for (i=0;i<vs.Count;i++)
            {
                EventBase eb = Instantiate(eventPrefab.gameObject, movesParent).GetComponent<EventBase>();
                EventBase.EventData d = new EventBase.EventData();
                d.time = vs[i].startTime;
                d.duration = vs[i].endTime - vs[i].startTime;
                d.value = vs[i];
                eb.eventData = d;
                eb.type = EventType.Move;
            }
            vs = judgeline.rotates;
            for (i=0;i<vs.Count;i++)
            {
                EventBase eb = Instantiate(eventPrefab.gameObject, rotatesParent).GetComponent<EventBase>();
                EventBase.EventData d = new EventBase.EventData();
                d.time = vs[i].startTime;
                d.duration = vs[i].endTime - vs[i].startTime;
                d.value = vs[i];
                eb.eventData = d;
                eb.type = EventType.Rotate;
            }
            List<SpeedEvent> sps = judgeline.speeds;
            for (i=0;i<sps.Count;i++)
            {
                EventBase eb = Instantiate(eventPrefab.gameObject, speedsParent).GetComponent<EventBase>();
                EventBase.EventData d = new EventBase.EventData();
                d.time = sps[i].startTime;
                d.duration = sps[i].endTime - sps[i].startTime;
                Z1029.NoteGlobalView.ValueSet v = new Z1029.NoteGlobalView.ValueSet();
                v.start = sps[i].value;
                d.value = v;
                eb.eventData = d;
                eb.type = EventType.Speed;
            }
        }

        private void Update()
        {
            if (LevelController.instance.showBeatline)
                UpdateBeatline();


        }

        public void InitBeatlines()
        {
            beatlines = new Beatline[20];
            for (int i = 0; i < 20; i++)
            {
                GameObject g = Instantiate(beatline, transform);
                Beatline b = g.GetComponent<Beatline>();
                beatlines[i] = b;
                b.index = i;
                b.Init(judgeline);
            }
            litBeatlines = new Beatline[20 * 32];
            for (int i = 0; i < 20; i++)
            {
                for (int j = 1; j < 32; j++)
                {
                    GameObject g = Instantiate(litBeatline, transform);
                    Beatline b = g.GetComponent<Beatline>();
                    litBeatlines[i * 32 + j] = b;
                    b.index = i;
                    b.onDensityIn = j;
                    b.Init(judgeline);
                }
            }
        }

        public void UpdateBeatline()
        {
            for (int i = 0; i < 20; i++)
            {
                Beatline b = beatlines[i];
                b.index = i + 1 + Mathf.RoundToInt(LevelController.instance.NowBeatTime / LevelController.density);
            }

            for (int i = 0; i < 20; i++)
            {
                for (int j = 1; j < 32; j++)
                {
                    Beatline b = litBeatlines[i * 32 + j];
                    b.index = i + Mathf.RoundToInt(LevelController.instance.NowBeatTime / LevelController.density);
                }
            }
        }

    }
}
